﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Qu.SeniorProject.Model;
using Qu.SeniorProject.WebUI.Utils;
using Omu.Awesome.Mvc;
using Qu.SeniorProject.Model.Repository;

namespace Qu.SeniorProject.WebUI.Controllers
{
    public class StudentsLookupController : LookupController
    {
        //private readonly IRepo<Student> repo;

        //public StudentsLookupController() //IRepo<Student> repo)
        //{
        //    this.repo = new StudentRepo();
        //}

        [HttpPost]
        public ActionResult Search(string search, IEnumerable<string> selected)
        {
            var result = new List<Student>().AsEnumerable();
            if (!string.IsNullOrEmpty(search))
                result = StudentRepository.GetStudentsEligibleToSeniorProject(search);
                    //StudentRepository.GetAll().Where(o => o.StudentId.Containz(search) || o.FirstName.Containz(search) || o.LastName.Containz(search));

            return View(@"Awesome\LookupList", result.Where(o => selected == null || !selected.Contains(o.StudentId)).ToList());
        }

        public ActionResult Get(string id)
        {
            var student = StudentRepository.Get(id);
            return student == null ? Content("Student deleted") : Content(student.ToString());
        }

        #region needed only if you use multiselect = true
        public ActionResult Selected(IEnumerable<string> selected)
        {
            return View(@"Awesome\LookupList", StudentRepository.GetAll().Where(o => selected != null && selected.Contains(o.StudentId)));
        }

        public ActionResult GetMultiple(IEnumerable<string> selected)
        {
            return Json(StudentRepository.GetAll().Where(o => selected.Contains(o.StudentId)).Select(v => new { Text = v.ToShortString() }));
        }
        #endregion

    }
}
